package com.wst.system.util;


import com.wst.system.security.entity.SelfUserEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/11 15:05
 * @since JDK 1.8
 */
@Slf4j
public class SecurityUtil {

    /**
     * 私有化构造器
     */
    private SecurityUtil(){}

    /**
     * 获取当前用户信息
     */
    public static SelfUserEntity getUserInfo(){
        if(SecurityContextHolder.getContext().getAuthentication() == null){
            return null;
        }
        Object obj=SecurityContextHolder.getContext().getAuthentication() .getPrincipal();
        if(obj instanceof SelfUserEntity) {
            SelfUserEntity userDetails = (SelfUserEntity) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return userDetails;
        }else {
            return null;
        }
    }
    /**
     * 获取当前用户ID
     */
    public static Integer getUserId(){
        return getUserInfo().getUserId();
    }
    /**
     * 获取当前用户账号
     */
    public static String getUserName(){
        return getUserInfo().getUsername();
    }
}
